package bsts.view.customer;

import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import bsts.application.BSTSApplication;
import bsts.model.Customer;

public class ViewUpdCustomerPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private static final String VIEW = "VIEW";
	private static final String UPD = "UPD";

	private JFrame containerParent;
	private JFrame container;
	private BSTSApplication manager;
	private Customer customer;

	private JPanel pnlMain;
	private JPanel pnlViewUpdPanel;
	private JPanel pnlButtons;
	private JPanel pnlBtnView;
	private JPanel pnlBtnUpd;
	private ViewCustomerPanel viewCustomerPanel;
	private UpdCustomerPanel updCustomerPanel;

	private JButton btnOk;
	private JButton btnUpdate;
	private JButton btnSave;
	private JButton btnCancel;

	private ActionListener actlis_btnUpdate;
	private ActionListener actlis_btnOk;
	private ActionListener actlis_btnSave;
	private ActionListener actlis_btnCancel;

	public ViewUpdCustomerPanel(JFrame containerParent, JFrame container,
			BSTSApplication manager, Customer customer) {
		this.containerParent = containerParent;
		this.container = container;
		this.manager = manager;
		this.customer = customer;

		pnlMain = new JPanel();
		pnlViewUpdPanel = new JPanel();
		pnlButtons = new JPanel();
		pnlBtnView = new JPanel();
		pnlBtnUpd = new JPanel();
		viewCustomerPanel = new ViewCustomerPanel(customer);
		updCustomerPanel = new UpdCustomerPanel(customer);

		pnlMain.setLayout(new GridLayout(2, 1));
		pnlViewUpdPanel.setLayout(new CardLayout());
		pnlButtons.setLayout(new CardLayout());
		pnlBtnView.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pnlBtnUpd.setLayout(new FlowLayout(FlowLayout.RIGHT));

		btnOk = new JButton("Ok");
		btnUpdate = new JButton("Update");
		btnSave = new JButton("Save");
		btnCancel = new JButton("Cancel");

		pnlViewUpdPanel.add(viewCustomerPanel, VIEW);
		pnlViewUpdPanel.add(updCustomerPanel, UPD);

		if (manager.isAdminRole())
			pnlBtnView.add(btnUpdate);
		pnlBtnView.add(btnOk);

		pnlBtnUpd.add(btnSave);
		pnlBtnUpd.add(btnCancel);

		pnlButtons.add(pnlBtnView, VIEW);
		pnlButtons.add(pnlBtnUpd, UPD);

		pnlMain.add(pnlViewUpdPanel);
		pnlMain.add(pnlButtons);

		this.add(pnlMain);

		initActionListener();
	}

	private void initActionListener() {
		actlis_btnUpdate = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout clViewUpd = (CardLayout) pnlViewUpdPanel.getLayout();
				CardLayout clButtons = (CardLayout) pnlButtons.getLayout();

				clViewUpd.show(pnlViewUpdPanel, UPD);
				clButtons.show(pnlButtons, UPD);
			}
		};

		actlis_btnOk = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.getViewManager().goBackParent(containerParent,
						container);
			}
		};

		actlis_btnSave = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				customer = updCustomerPanel.updateCustomer();
				manager.getCustomerManager().updateCustomer(customer);

				CardLayout clViewUpd = (CardLayout) pnlViewUpdPanel.getLayout();
				CardLayout clButtons = (CardLayout) pnlButtons.getLayout();

				viewCustomerPanel.refresh(customer);
				clViewUpd.show(pnlViewUpdPanel, VIEW);
				clButtons.show(pnlButtons, VIEW);
			}
		};

		actlis_btnCancel = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout clViewUpd = (CardLayout) pnlViewUpdPanel.getLayout();
				CardLayout clButtons = (CardLayout) pnlButtons.getLayout();

				clViewUpd.show(pnlViewUpdPanel, VIEW);
				clButtons.show(pnlButtons, VIEW);
			}
		};

		btnUpdate.addActionListener(actlis_btnUpdate);
		btnOk.addActionListener(actlis_btnOk);
		btnSave.addActionListener(actlis_btnSave);
		btnCancel.addActionListener(actlis_btnCancel);
	}

}
